﻿@Styles.Render("~/bundles/jqgridcss")
@Scripts.Render("~/bundles/jqgridjs")
@using CNKI.TPI.Web.Search.Model;

<script type="text/javascript">
    var lastSelection;//最近一次编辑行
    saveparameters = {
        "successfunc": null,
        "url": null,
        "extraparam": {},
        "aftersavefunc": null,
        "errorfunc": null,
        "afterrestorefunc": null,
        "restoreAfterError": true,
        "mtype": "POST"
    }//保存参数
    var editstatus;//编辑标识
    //移动
    function moveLeft() {
        var str = "";
        str = $("#jqGridLeft").jqGrid('getGridParam', 'selarrrow');
        if (str != "") {
            var arrayid = new Array();
            arrayid = str.toString().split(',');
            len = arrayid.length;
            var m = 0;
            var seldbname = "";
            //判断是右侧是否已经包含选中的数据库
            for (var i = 0; i < len; i++) {
                var m = 0;
                var obj = $("#jqGrid").jqGrid("getRowData");
                $(obj).each(function () {
                    if (this.DBCode == arrayid[i]) {
                        m = 1;
                        seldbname += this.DBName + ",";
                    }
                })
                if (m == 0) {
                    var ret = jQuery("#jqGridLeft").jqGrid('getRowData', arrayid[i]);
                    var dbid  = ret.SysID;
                    var dbcode = ret.DatabaseCode;
                    var dbName = ret.DatabaseName;
                    var datarow = { DBCode: dbcode, DBName: dbName, DBID: dbid};
                    var gridlen = jQuery('#jqGrid').jqGrid('getGridParam', 'records')
                    var su = jQuery("#jqGrid").jqGrid('addRowData', gridlen + 1, datarow);
                }

            }
            if (seldbname != "") {
                messageAlert(seldbname + " 已经存在右侧列表！");
            }
            //取消选择
            for (var m = 0; m < len; m++) {
                jQuery("#jqGridLeft").jqGrid('setSelection', arrayid[m]);
            }
        }
        else {
            messageAlert("请选择要移动的行！");
        }
    }
    //加载右侧数据
    function loadright() {
        var urlright = "@Html.Raw(Url.Action("DBList", "Admin_Topic"))?sysid=" + escape($("#sysid").val());
        $("#jqGrid").jqGrid({
            url: timestamp(urlright),
            editurl: 'clientArray',
            mtype: "GET",
            datatype: "json",
            page: 1,
            colModel: [
                {
                    label: '数据库代码',
                    name: 'DBCode',
                    hidden: true
                },
                {
                    label: '数据库ID',
                    name: 'DBID',
                    hidden: true
                },
                 {
                     label: '数据库名称',
                     name: 'DBName',
                     width: 156,
                     sortable: false
                 }
            ],
            loadonce: true,
            onSelectRow: editRow, // the javascript function to call on row click. will ues to to put the row in edit mode
            viewrecords: true,
            width: 256,
            height: 328,
            scroll: 1,
            rowNum: "all",
            recordtext: "<a class=\"btn btn-s btn-warn-f fr deleteright\" style=\" margin-top: 3px;color: #fff\" onclick=\"delprm()\">删除</a><label>共{2}条</label>",
            emptyrecords: "暂无数据",
            loadtext: "加载中",
            multiselect: true,
            pager: "#jqGridPager"

        });

    }
    //删除右侧记录方法
    function delprm() {
        //var consoleDlg = $("#delcostListDlg");
        var selectedRowIds = $("#jqGrid").jqGrid("getGridParam", "selarrrow");
        var len = selectedRowIds.length;
        if (len <= 0) {
            messageAlert("请选择要删除的记录！");
            return;
        }
        confirmDialog('确定要删除吗?', function () {
            for (var i = 0; i < len ; i++) {
                $("#jqGrid").jqGrid("delRowData", selectedRowIds[0]);
            }
            $("#cb_jqGrid").removeAttr("checked", "checked");
        })
    }
    //编辑行
    function editRow(id) {
        var grid = $("#jqGrid");

        if ((id && id !== lastSelection) || editstatus == 0) {
            editstatus = 1;//编辑模式
            grid.jqGrid('editRow', id, { keys: true, focusField: 3 });
            if (id !== lastSelection) {
                grid.jqGrid('saveRow', lastSelection, saveparameters);
            }
            lastSelection = id;
        }
        else { //修改之后再次点击当前行则保存
            if (editstatus == 1) {
                grid.jqGrid('saveRow', lastSelection, saveparameters);
                editstatus = 0;//保存模式
            }
        }
    }
    $(document).ready(function () {
        loadright();//右侧列表
        //左侧列表
        var urlleft = "@Html.Raw(Url.Action("GetPublishDB", "Admin_MultiDBPublish"))";
        $("#jqGridLeft").jqGrid({
            url: timestamp(urlleft),
            editurl: 'clientArray',
            mtype: "GET",
            datatype: "json",
            page: 1,
            colModel: [
                {
                    label: '数据库代码',
                    name: 'DatabaseCode',
                    hidden: true
                },
                {
                    label: '数据库ID',
                    name: 'SysID',
                    hidden: true
                },
                 {
                     label: '数据库名称',
                     name: 'DatabaseName',
                     width: 200,
                     sortable: false
                 }
            ],
            loadonce: true,
            viewrecords: true,
            width: 256,
            height: 353,
            scroll: 1,
            rowNum: "all",
            multiselect: true,
            recordtext: "共{2}条",
            emptyrecords: "暂无数据",
            loadtext: "加载中",
            //pgtext: "页码 {0} of {1}",
            pager: "#jqGridPagerLeft"
        });


    });
    //分类发生变化
    function classChange() {
        var thisvalue = $("#publishClass").val();
        $("#classcode").val(thisvalue);
        var urlright = "@Html.Raw(Url.Action("DBList", "Admin_MultiDBPublish"))?dbcode=" + escape(thisvalue);
        $('#jqGrid').jqGrid('clearGridData');
        $('#jqGrid').jqGrid('setGridParam', { url: timestamp(urlright), datatype: 'json', page: 1 }).trigger('reloadGrid');
    }
    //保存
    function saveDB() {
        disablesave();
        $("#jqGrid").jqGrid('saveRow', lastSelection, saveparameters);//保存最后编辑的行
        var obj = $("#jqGrid").jqGrid("getRowData");
        if (obj != null && obj.length <= 0) {
            messageAlert("请添加该分类下的数据库！");
            enablesave();
            return;
        }
        var dbcode = [];
        var dbname = [];
        var dbid = [];
        $(obj).each(function () {
            dbcode.push(this.DBCode);
            dbname.push(this.DBName);
            dbid.push(this.DBID);
        })
        var urlstr = "@Html.Raw(Url.Action("SaveDBList", "Admin_Topic"))";
        ajaxinfo(urlstr, dbcode, dbname, dbid);
    }
    //调用增删改查方法
    function ajaxinfo(urlaction, dbcode, dbname, dbid) {
        var dataold = { "sysid": $("#sysid").val(), "name": $("#name").val(), "status": $("#status").val(), "dbcode": dbcode.join(), "dbname": dbname.join(), "dbid": dbid.join() };
        $.ajax({
            type: "POST",
            url: urlaction,
            contentType: "application/json",
            data: JSON.stringify(dataold),
            success: function (msg) {
                if (msg == "True") {
                    messageAlert("操作成功！");

                }
                else {
                    messageAlert("操作失败！");
                }
                enablesave();
            },
            error: function (e)
            { enablesave(); }
        })
    }
    function disablesave() {
        $(".btn-warn-f").attr("disabled", "disabled");
    }
    function enablesave() {
        $(".btn-warn-f").removeAttr("disabled", "disabled");
    }
</script>
<style>
    .ui-jqgrid .ui-jqgrid-htable th div {
        overflow: visible;
        height: 30px;
    }

    .ui-jqgrid .ui-jqgrid-sortable {
        text-decoration: none;
        cursor: default;
    }
</style>
<input type="hidden" id="sysid" value="@ViewBag.SYSID" hidden="hidden" />
<input type="hidden" id="name" value="@ViewBag.Name" hidden="hidden" />
<input type="hidden" id="status" value="@ViewBag.status" hidden="hidden" />
<h3 class='box-h'>添加单库</h3>
<div class="wrapR-c wrapR-c2 pt20 mt20">
    <div class="clearfix posr wrapR-c2-in">
        <div class="wrapR-c-box wrapR-c-l fl posr" style="overflow-x: visible; overflow-y: visible">
            <div class="wrapR-c-box-t">已发布单库列表</div>
            <div class="wrapR-c-box-b" style="padding-left: 1px">
                <table id="jqGridLeft"></table>
                <div id="jqGridPagerLeft"></div>
            </div>
        </div>
        <div class="wrapR-c-m tc posa"><a class="iconfont-s iconfont-right-triangle-s" href="javascript:void(0);" onclick="moveLeft()"></a></div>
        <div class="wrapR-c-box wrapR-c-r fr posr" style="overflow-x: visible; overflow-y: visible">
            <div class="wrapR-c-box-t">数据源数据库列表</div>
            <div class="wrapR-c-box-b" style="padding-left: 1px">
                <table id="jqGrid"></table>
                <div id="jqGridPager">
                </div>

            </div>

        </div>
    </div>

    <div class="tc mt30">

        <button class="btn btn-m btn-prim-f" onclick="ajaxLoad('initload', '@Html.Raw(Url.Action("DBManager", "Admin_Topic"))')">返回</button>
        <button class="btn btn-m btn-submit btn-warn-f" style="margin-left:30px;" onclick="saveDB()">保存</button>
    </div>
</div>
